package com.deer.wms.base.system.web;

import com.deer.wms.base.system.model.Test;
import com.deer.wms.base.system.model.TestCriteria;
import com.deer.wms.base.system.service.TestService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;

import java.util.List;

/**
* Created by guo on 2020/08/08.
*/
@RestController
@RequestMapping("/tests")
public class TestController {

    @Autowired
    private TestService testService;

    @PostMapping("/add")
    public Result add(@RequestBody Test test) {
        testService.save(test);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/delete")
    public Result delete(  Integer id) {
        testService.removeById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Test test) {
        testService.updateById(test);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/{id}")
    public Result detail(@PathVariable Integer id) {
        Test test = testService.getById(id);
        return ResultGenerator.genSuccessResult(test);
    }

    @GetMapping("/list")
    public Result list(TestCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<Test> list = testService.findList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

}
